//
//  KDAuthorSetController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/3.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDAuthorSetController.h"
#import "GBAuthorNameViewController.h"
#import "GBNicknameViewController.h"
#import "GBPersonalHomepageViewController.h"
#import "AFNetworking.h"
#import "KDMSettingCellModel.h"
#import "SVProgressHUD.h"

#define labelText @"温馨提示：请逐个添加主页，二者的搜索机制是OR(或者)。"

@interface KDAuthorSetController ()<UITextFieldDelegate, ReturnTheResult, NicknameReturnResult, HomepageReturnResult>
{
    //11.4
    UIButton *shareBtn; //单位共享的btn
    UIButton *saveButton; //保存按钮
    NSMutableArray *nicknameArray; //昵称的数组
    NSMutableArray *homepageArray; //个人主页的数组
    
    //11.7
    UILabel *nameLabel; //作者名称label
    UILabel *nameSelectLabel; //作者名称已选择label
    UILabel *nicknameLabel; //昵称label
    UILabel *nicknameSelectLabel; //昵称已选择label
    UILabel *personalHomepageLabel; //个人主页label
    UILabel *personalHomepageSelectLabel; //个人主页已选择label
    float height; //leble的高度
    UILabel *personalHomepageHintLabel; //个人主页提示的label
    NSString *titleName; //作者名称
    NSString *titleNameCopy; //作者名称保存
    
    //11.10
    NSMutableArray *nicknameIdArray; //昵称ID的数组
    NSMutableArray *homepageIdArray; //个人主页id的数组
    
    //11.11
    NSString *shareIs; //判断是否点击选中
    
    //1.4
    NSString *selectY; //是否选中
}
@end

@implementation KDAuthorSetController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor = WQColor(247, 249, 250);
    nicknameArray = [NSMutableArray array];
    nicknameIdArray = [NSMutableArray array];
    homepageArray = [NSMutableArray array];
    homepageIdArray = [NSMutableArray array];
    
    //判断请求数据
    [self requestTheData];
    
    //创建保存按钮
    [self createSaveBtn];
    // Do any additional setup after loading the view.
}

#pragma mark - 判断请求数据
-(void)requestTheData
{
    if([_type isEqualToString:@"add"])
    {
        //创建主界面
        [self createMainView];
    }
    else
    {
        //titleName = [NSString stringWithFormat:@"%@", _model];
        //编辑界面进来的，请求数据
        KDAccount *account = [KDAccountTool account];
        NSString *saveUrl = [NSString stringWithFormat:@"%@%@%@%@%ld", ServerActionAddr, @"&method=getAuthorMonitorSetting&token=",account.tokens,@"&a_id=",(long)_model.m_Id];
        //NSLog(@"%@",saveUrl);
        //请求数据
        NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:saveUrl]];
        
        AFHTTPRequestOperation *reqOper = [[AFHTTPRequestOperation alloc] initWithRequest:httpReq];
        [reqOper setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op,id success){
            NSHTTPURLResponse *response = [op response];
            NSInteger code = [response statusCode];
            if (code == 200)
            {
                NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[op responseData] options:NSJSONReadingMutableContainers error:nil];
                if([jsonDict[@"status"] isEqualToString:@"ok"])
                {
                    //取出作者名称
                    titleName = [NSString stringWithFormat:@"%@", jsonDict[@"authorMonitor"][@"a_Name"]];
                    titleNameCopy = [NSString stringWithFormat:@"%@", jsonDict[@"authorMonitor"][@"a_Name"]];
    
                    int selec = [jsonDict[@"authorMonitor"][@"a_Share"] intValue];
                    if(selec == 1)
                    {
                        shareIs = @"yes";
                    }
                    else
                    {
                        shareIs = @"no";
                    }
                    
                    //取出昵称
                    if ([jsonDict[@"nicks"] isKindOfClass:[NSNull class]])
                    {
                        
                    }
                    else
                    {
                        NSMutableArray *info = jsonDict[@"nicks"];
                        for(NSDictionary *dic in info)
                        {
                            //取出昵称id
                            int num = [dic[@"authId"] intValue];
                            NSString *str = [NSString stringWithFormat:@"%d", num];
                            [nicknameIdArray addObject:str];
                        
                            //取出昵称
                            NSString *nick = [NSString stringWithFormat:@"%@", dic[@"nick"]];
                            [nicknameArray addObject:nick];
                        }
                    }
                    
                    if ([jsonDict[@"homeUrls"] isKindOfClass:[NSNull class]])
                    {
                        
                    }
                    else
                    {
                        //取出个人主页
                        NSMutableArray *homepage = jsonDict[@"homeUrls"];
                        for(NSDictionary *dic in homepage)
                        {
                            //取出昵称id
                            int num = [dic[@"entryId"] intValue];
                            NSString *str = [NSString stringWithFormat:@"%d", num];
                            [homepageIdArray addObject:str];
                            
                            //取出昵称
                            NSString *nick = [NSString stringWithFormat:@"%@", dic[@"homeUrl"]];
                            [homepageArray addObject:nick];
                            //NSLog(@"%@", homepageArray);
                        }
                    }
                    
                    //创建主界面
                    [self performSelector:@selector(createMainView) withObject:nil afterDelay:0.8];
                }
                //账号在别处登录
                else if([jsonDict[@"status"] isEqualToString:@"notlogin"])
                {
                    [SVProgressHUD showErrorWithStatus:@"该账号已在其他地方登陆" maskType:SVProgressHUDMaskTypeGradient];
                    
                    KDNewfeatureController *login = [[KDNewfeatureController alloc] init];
                    //跳转到登陆界面
                    //设置切换风格
                    login.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                    //2.切换控制器
                    [self presentViewController:login animated:YES completion:nil];
                }
                //保存失败
                else if ([jsonDict[@"status"] isEqualToString:@"err"])
                {
                    [SVProgressHUD showErrorWithStatus:@"请求网络失败，请检查网络" maskType:SVProgressHUDMaskTypeGradient];
                }
            }
        } failure:^(AFHTTPRequestOperation *op, NSError *error) {
            if (error) {
                //NSLog(@"req error %@",error);
            }
        }];
        [reqOper start];
    }
    //创建主界面
    //[self createMainView];
}

#pragma mark - 计算label的高度
-(void)countTheHeight
{
    CGFloat contentW = screenW - personalHomepageHintLabel.frame.origin.x;
    //label的字体 HelveticaNeue  Courier
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:12.0f];
    personalHomepageHintLabel.font = fnt;
    personalHomepageHintLabel.numberOfLines = 0;
    personalHomepageHintLabel.lineBreakMode = NSLineBreakByWordWrapping;
    //iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
    CGRect tmpRect = [labelText boundingRectWithSize:CGSizeMake(contentW, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil];
    
    //高度H
    CGFloat contentH = tmpRect.size.height;
    height = contentH;
}

#pragma mark - 创建主界面
-(void)createMainView
{
    //创建作者名称界面
    [self createAuthorView];
    
    //创建昵称界面
    [self createNicknameView];
    
    //创建个人主页界面
    [self createPersonalHomepageView];
}

#pragma mark - 创建作者名称界面
-(void)createAuthorView
{
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 20, screenW, 40)];
    view1.backgroundColor = [UIColor whiteColor];
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, screenW - 85, 40)];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    
    nameSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenW - 80, 0, 40, 40)];
    nameSelectLabel.font = [UIFont systemFontOfSize:12];
    nameSelectLabel.textColor = [UIColor lightGrayColor];
    nameSelectLabel.textAlignment = NSTextAlignmentCenter;
    
    if([_type isEqualToString:@"add"])
    {
        nameLabel.text = @"作者名称";
        nameSelectLabel.text = @"未选择";
    }
    else
    {
        nameLabel.text = titleName;
        nameSelectLabel.text = @"已选择";
    }
    
    UIImageView *arrawImageview = [[UIImageView alloc] initWithFrame:CGRectMake(screenW - 40, 0, 40, 40)];
    arrawImageview.image = [UIImage imageNamed:@"rightArraw"];
    
    UIButton *nameBtn = [UIButton buttonWithType:0];
    nameBtn.frame = CGRectMake(0, 0, screenW, 40);
    nameBtn.backgroundColor = [UIColor clearColor];
    [nameBtn addTarget:self action:@selector(GotoNameSelect) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, screenW, 25)];
    hintLabel.backgroundColor = WQColor(247, 249, 250);
    hintLabel.font = [UIFont systemFontOfSize:12];
    hintLabel.textColor = [UIColor lightGrayColor];
    hintLabel.text = @"请输入作者名称。";
    
    UILabel *hintLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(150, 87, 150, 25)];
    hintLabel2.backgroundColor = WQColor(247, 249, 250);
    hintLabel2.font = [UIFont systemFontOfSize:12];
    hintLabel2.textColor = [UIColor lightGrayColor];
    hintLabel2.text = @"本单位成员可查看此监测。";
    
    shareBtn = [UIButton buttonWithType:0];
    shareBtn.frame = CGRectMake(10, 87, 80, 26);
    shareBtn.backgroundColor = [UIColor clearColor];
    [shareBtn setTitle:@"单位共享" forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [shareBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(ShareSelectAction) forControlEvents:UIControlEventTouchUpInside];

    if ([_type isEqualToString:@"add"])
    {
        [shareBtn setImage:[UIImage imageNamed:@"check_normal"] forState:UIControlStateNormal];
        [shareBtn setImage:[UIImage imageNamed:@"check_checked"] forState:UIControlStateSelected];
    }
    else
    {
        KDAccount *account = [KDAccountTool account];
        if(account.userId == _model.UID)
        {
            [shareBtn setImage:[UIImage imageNamed:@"check_normal"] forState:UIControlStateNormal];
            [shareBtn setImage:[UIImage imageNamed:@"check_checked"] forState:UIControlStateSelected];

            [shareBtn setEnabled:YES];
            if([shareIs isEqualToString:@"yes"])
            {
                shareBtn.selected = YES;
            }
            else
            {
                shareBtn.selected = NO;
            }
        }
        else
        {
            if([shareIs isEqualToString:@"yes"])
            {
                [shareBtn setImage:[UIImage imageNamed:@"check_disable"] forState:  UIControlStateDisabled];
            }
            [shareBtn setEnabled:NO];
        }
    }
    
    [self.view addSubview:view1];
    [view1 addSubview:nameLabel];
    [view1 addSubview:nameSelectLabel];
    [view1 addSubview:arrawImageview];
    [view1 addSubview:nameBtn];
    [self.view addSubview:hintLabel];
    //[self.view addSubview:hintLabel2];
    [self.view addSubview:shareBtn];
}

#pragma mark - 单位分享按钮的操作
-(void)ShareSelectAction
{
    shareBtn.selected = !shareBtn.selected;
}

#pragma mark - 创建昵称界面
-(void)createNicknameView
{
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 115, screenW, 40)];
    view1.backgroundColor = [UIColor whiteColor];
    
    nicknameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, screenW - 85, 40)];
    nicknameLabel.textAlignment = NSTextAlignmentLeft;
    
    nicknameSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenW - 80, 0, 40, 40)];
    nicknameSelectLabel.font = [UIFont systemFontOfSize:12];
    nicknameSelectLabel.textColor = [UIColor lightGrayColor];
    nicknameSelectLabel.textAlignment = NSTextAlignmentCenter;
    
    if([_type isEqualToString:@"add"])
    {
        nicknameLabel.text = @"昵称";
        nicknameSelectLabel.text = @"未选择";
    }
    else
    {
        if (nicknameArray.count == 0 || nicknameArray == nil)
        {
            nicknameLabel.text = @"昵称";
            nicknameSelectLabel.text = @"未选择";
        }
        else
        {
            nicknameLabel.text = nicknameArray[0];
            nicknameSelectLabel.text = @"已选择";
        }
    }
    
    UIImageView *arrawImageview = [[UIImageView alloc] initWithFrame:CGRectMake(screenW - 40, 0, 40, 40)];
    arrawImageview.image = [UIImage imageNamed:@"rightArraw"];
    
    UIButton *nameBtn = [UIButton buttonWithType:0];
    nameBtn.frame = CGRectMake(0, 0, screenW, 40);
    nameBtn.backgroundColor = [UIColor clearColor];
    [nameBtn addTarget:self action:@selector(GotoNicknameSelect) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 155, screenW, 25)];
    hintLabel.backgroundColor = WQColor(247, 249, 250);
    hintLabel.font = [UIFont systemFontOfSize:12];
    hintLabel.textColor = [UIColor lightGrayColor];
    hintLabel.text = @"温馨提示：请逐个添加昵称，二者的搜索机制是OR(或者)。";
    
    [self.view addSubview:view1];
    [view1 addSubview:nicknameLabel];
    [view1 addSubview:nicknameSelectLabel];
    [view1 addSubview:arrawImageview];
    [view1 addSubview:nameBtn];
    [self.view addSubview:hintLabel];
}

#pragma mark - 创建个人主页界面
-(void)createPersonalHomepageView
{
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 185, screenW, 40)];
    view1.backgroundColor = [UIColor whiteColor];
    
    personalHomepageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, screenW - 85, 40)];
    personalHomepageLabel.textAlignment = NSTextAlignmentLeft;
    
    personalHomepageSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenW - 80, 0, 40, 40)];
    personalHomepageSelectLabel.font = [UIFont systemFontOfSize:12];
    personalHomepageSelectLabel.textColor = [UIColor lightGrayColor];
    personalHomepageSelectLabel.textAlignment = NSTextAlignmentCenter;
    
    if([_type isEqualToString:@"add"])
    {
        personalHomepageLabel.text = @"个人主页";
        personalHomepageSelectLabel.text = @"未选择";
    }
    else
    {
        if (homepageArray.count == 0 || homepageArray == nil)
        {
            personalHomepageLabel.text = @"个人主页";
            personalHomepageSelectLabel.text = @"未选择";
        }
        else
        {
            personalHomepageLabel.text = homepageArray[0];
            personalHomepageSelectLabel.text = @"已选择";
        }
    }
    
    UIImageView *arrawImageview = [[UIImageView alloc] initWithFrame:CGRectMake(screenW - 40, 0, 40, 40)];
    arrawImageview.image = [UIImage imageNamed:@"rightArraw"];
    
    UIButton *nameBtn = [UIButton buttonWithType:0];
    nameBtn.frame = CGRectMake(0, 0, screenW, 40);
    nameBtn.backgroundColor = [UIColor clearColor];
    [nameBtn addTarget:self action:@selector(GotoPersonalHomepageSelect) forControlEvents:UIControlEventTouchUpInside];
    
    personalHomepageHintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 225, screenW - 10, 0)];
    [self countTheHeight];
    personalHomepageHintLabel.backgroundColor = WQColor(247, 249, 250);
    personalHomepageHintLabel.font = [UIFont systemFontOfSize:12];
    personalHomepageHintLabel.textColor = [UIColor lightGrayColor];
    personalHomepageHintLabel.text = labelText;
    personalHomepageHintLabel.frame = CGRectMake(10, 220, screenW, height + 10);
    
    [self.view addSubview:view1];
    [view1 addSubview:personalHomepageLabel];
    [view1 addSubview:personalHomepageSelectLabel];
    [view1 addSubview:arrawImageview];
    [view1 addSubview:nameBtn];
    [self.view addSubview:personalHomepageHintLabel];
}

#pragma mark - 跳转输入作者名称的界面
-(void)GotoNameSelect
{
    GBAuthorNameViewController *authorNameController = [[GBAuthorNameViewController alloc] init];
    authorNameController.delegateReturn = self;
    if([_type isEqualToString:@"add"])
    {
        authorNameController.titleAuthorName = @"";
    }
    else
    {
        authorNameController.titleAuthorName = titleNameCopy;
    }
    
    KDAccount *account = [KDAccountTool account];
    if(account.userId == _model.UID)
    {
        authorNameController.isSelf = @"isSelf";
    }
    else
    {
        authorNameController.isSelf = @"noSelf";
    }
    [self.navigationController pushViewController:authorNameController animated:YES];
}

#pragma mark - 返回输入作者名称界面的结果代理
-(void)returnTheResult:(NSString *)title
{
    titleName = [NSString stringWithFormat:@"%@", title];
    titleNameCopy = [NSString stringWithFormat:@"%@", title];
    //NSLog(@"12111%@", titleNameCopy);
}

#pragma mark - 跳转输入昵称的界面
-(void)GotoNicknameSelect
{
    GBNicknameViewController *nicknameController = [[GBNicknameViewController alloc] init];
    nicknameController.delecateNickname = self;
    
    nicknameController.dataArray = [NSMutableArray arrayWithArray:nicknameArray];
    nicknameController.dataIdArray = [NSMutableArray arrayWithArray:nicknameIdArray];
    [self.navigationController pushViewController:nicknameController animated:YES];
}

#pragma mark - 跳转输入个人主页的界面
-(void)GotoPersonalHomepageSelect
{
    GBPersonalHomepageViewController *personalHomepageController =[[GBPersonalHomepageViewController alloc] init];
    personalHomepageController.delegateHomepage = self;
    
    personalHomepageController.dataArray = [NSMutableArray arrayWithArray:homepageArray];
    personalHomepageController.dataIdArray = [NSMutableArray arrayWithArray:homepageIdArray];
    [self.navigationController pushViewController:personalHomepageController animated:YES];
}

#pragma mark - 昵称界面返回的数据代理
-(void)nicknameReturnResult:(NSMutableArray *)array and:(NSMutableArray *)nicknameId
{
    nicknameArray = [NSMutableArray arrayWithArray:array];
    nicknameIdArray = [NSMutableArray arrayWithArray:nicknameId];
}

#pragma mark - 个人主页界面返回的数据代理
-(void)homepageReturnResult:(NSMutableArray *)array and:(NSMutableArray *)nameIdArray
{
    homepageArray = [NSMutableArray arrayWithArray:array];
    homepageIdArray = [NSMutableArray arrayWithArray:nameIdArray];
}

#pragma mark - 创建保存按钮
-(void)createSaveBtn
{
    saveButton = [UIButton buttonWithType:0];
    saveButton.frame = CGRectMake(10, 280, screenW - 20, 35);
    saveButton.layer.cornerRadius = 5;
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    saveButton.backgroundColor = KDHeadColor;
    [saveButton addTarget:self action:@selector(SaveTheData) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveButton];
}

#pragma mark - 保存按钮点击操作
-(void)SaveTheData
{
    if(titleName == NULL || [titleName isEqualToString:@""])
    {
        [SVProgressHUD showErrorWithStatus:@"作者名称输入不能为空" maskType:SVProgressHUDMaskTypeGradient];
        return;
    }
    if(nicknameArray.count == 0 || nicknameArray == NULL)
    {
        [SVProgressHUD showErrorWithStatus:@"昵称输入不能为空" maskType:SVProgressHUDMaskTypeGradient];
        return;
    }
    if(homepageArray.count == 0 || homepageArray == NULL)
    {
        [SVProgressHUD showErrorWithStatus:@"个人主页输入不能为空" maskType:SVProgressHUDMaskTypeGradient];
        return;
    }
    NSString *nicknameString = [NSString stringWithFormat:@"%@", nicknameArray[0]];
    NSString *nicknameIdString = [NSString stringWithFormat:@"%@", nicknameIdArray[0]];
    for(int i = 1; i < nicknameArray.count; i++)
    {
        nicknameString = [NSString stringWithFormat:@"%@,%@", nicknameString, nicknameArray[i]];
        nicknameIdString = [NSString stringWithFormat:@"%@,%@",nicknameIdString, nicknameIdArray[i]];
    }
    
    NSString *homepageUrl = [NSString stringWithFormat:@"%@", homepageArray[0]];
    NSString *homepageIdUrl = [NSString stringWithFormat:@"%@", homepageIdArray[0]];
    for(int i = 1; i < homepageArray.count; i++)
    {
        homepageUrl = [NSString stringWithFormat:@"%@,%@",homepageUrl, homepageArray[i]];
        homepageIdUrl = [NSString stringWithFormat:@"%@,%@",homepageIdUrl, homepageIdArray[i]];
    }
    
    KDAccount *account = [KDAccountTool account];
    
    if ([_type isEqualToString:@"add"])
    {
        if(shareBtn.isSelected)
        {
            selectY = @"1";
        }
        else
        {
            selectY = @"0";
        }
    }
    else
    {
        if(account.userId == _model.UID)
        {
            if(shareBtn.isSelected)
            {
                selectY = @"1";
            }
            else
            {
                selectY = @"0";
            }
        }
        else
        {
            selectY = @"1";
        }
    }
    
    //KDAccount *account = [KDAccountTool account];
    NSString *saveUrl = nil;
    NSDictionary *dic = nil;
    //转码
//    titleName = [titleName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    nicknameString = [nicknameString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    nicknameIdString = [nicknameIdString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    homepageUrl = [homepageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    homepageIdUrl = [homepageIdUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if([_type isEqualToString:@"add"])
    {
        saveUrl = [NSString stringWithFormat:@"%@%@%@", ServerActionAddr, @"&method=addAuthorMonitorSetting&token=",account.tokens];
        
        dic =@{@"name":titleName,@"share":selectY,@"nick":nicknameString,@"authId":nicknameIdString,@"homeUrl":homepageUrl,@"entryId":homepageIdUrl};
    }
    else
    {
        saveUrl = [NSString stringWithFormat:@"%@%@%@", ServerActionAddr, @"&method=saveAuthorMonitorSetting&token=",account.tokens];
        
        NSString *aid = [NSString stringWithFormat:@"%ld", (long)_model.m_Id];
        dic = @{@"name":titleName,@"share":selectY,@"nick":nicknameString,@"authId":nicknameIdString,@"homeUrl":homepageUrl,@"entryId":homepageIdUrl,@"a_id":aid};
    }
    
    //NSLog(@"%@, %@", saveUrl, dic);
    
    //进度圈提示进度
    [SVProgressHUD showWithStatus:@"正在保存" maskType:SVProgressHUDMaskTypeGradient ];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager POST:saveUrl parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
         //NSLog(@"%@", jsonDict);
         
         if([jsonDict[@"status"] isEqualToString:@"ok"])
         {
             if([jsonDict[@"info"] intValue] == 1)
             {
                 [SVProgressHUD showSuccessWithStatus:@"保存成功" maskType:SVProgressHUDMaskTypeGradient];
                 
                 [self.navigationController popViewControllerAnimated:YES];
             }
             else if([jsonDict[@"info"] intValue] == 0)
             {
                 [SVProgressHUD showErrorWithStatus:@"保存失败" maskType:SVProgressHUDMaskTypeGradient];
             }
             else if([jsonDict[@"info"] intValue] == -1)
             {
                 [SVProgressHUD showErrorWithStatus:@"已存在" maskType:SVProgressHUDMaskTypeGradient];
             }
         }
         //账号在别处登录
         else if([jsonDict[@"status"] isEqualToString:@"notlogin"])
         {
             [SVProgressHUD showErrorWithStatus:@"该账号已在其他地方登陆" maskType:SVProgressHUDMaskTypeGradient];
             
             KDNewfeatureController *login = [[KDNewfeatureController alloc] init];
             //跳转到登陆界面
             //设置切换风格
             login.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
             //2.切换控制器
             [self presentViewController:login animated:YES completion:nil];
         }
         //保存失败
         else if ([jsonDict[@"status"] isEqualToString:@"err"])
         {
             [SVProgressHUD showErrorWithStatus:@"保存失败" maskType:SVProgressHUDMaskTypeGradient];
         }
         //已存在
         else if ([jsonDict[@"status"] isEqualToString:@"ext"])
         {
             [SVProgressHUD showErrorWithStatus:@"已存在" maskType:SVProgressHUDMaskTypeGradient];
         }
         
         //进度条消失
         [SVProgressHUD dismiss];
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         //进度条消失
         [SVProgressHUD dismiss];
         [SVProgressHUD showErrorWithStatus:@"保存失败" maskType:SVProgressHUDMaskTypeGradient];
     }];
}

#pragma mark - 将选择的内容显示出来
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    if([_type isEqualToString:@"add"])
    {
        if(titleName == nil || [titleName isEqualToString:@""])
        {
            nameLabel.text = @"作者名称";
            nameSelectLabel.text = @"未选择";
        }
        else
        {
            nameLabel.text = titleName;
            nameSelectLabel.text = @"已选择";
        }
    }
    else
    {
        nameLabel.text = titleNameCopy;
        nameSelectLabel.text = @"已选择";
    }
    
    if(nicknameArray.count == 0)
    {
        nicknameLabel.text = @"昵称";
        nicknameSelectLabel.text = @"未选择";
    }
    else
    {
        nicknameLabel.text = [NSString stringWithFormat:@"%@", nicknameArray[0]];
        nicknameSelectLabel.text = @"已选择";
    }
    
    if(homepageArray.count == 0)
    {
        personalHomepageLabel.text = @"个人主页";
        personalHomepageSelectLabel.text = @"未选择";
    }
    else
    {
        personalHomepageLabel.text = [NSString stringWithFormat:@"%@", homepageArray[0]];
        personalHomepageSelectLabel.text = @"已选择";
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
